package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;
    @PostMapping("/login")
    //这里的R相当于之前的Result，加入HttpServletRequest request，
    // 存入session并返回登录页面
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        return employeeService.login (request,employee);

    }
    //管理员退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession ().removeAttribute ("employee");
        return R.success ("退出成功");
    }
    //新增员工
    @PostMapping
    public R<String>add(HttpServletRequest request,@RequestBody Employee employee){
        return employeeService.add (request,employee);
    }

    //员工信息分页查询
    @GetMapping("/page")
    //泛型对象里面要有total之类的属性
    public R<Page> page(int page,int pageSize,String name){
        return employeeService.page (page,pageSize,name);
    }
    //根据id查询
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
       return employeeService.getById (id);
    }
    //修改员工状态
    @PutMapping
    public R<String>update(HttpServletRequest request,@RequestBody Employee employee){
       return employeeService.update (request,employee);
    }

}
